package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.SysUserInfo;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.mapper.UserMapper;
import com.itheima.ydd.service.RoleService;
import com.itheima.ydd.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.CustomSQLErrorCodesTranslation;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @since 2022-02-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleService roleService;

    @Override
    public void saveWithRoleId(@RequestBody SysUserInfo sysUserInfo) {
        User user = new User();
        String stringRoleIds = getStringRoleIds(sysUserInfo);
        sysUserInfo.setPassword(DigestUtils.md5DigestAsHex(sysUserInfo.getPassword().getBytes()));
        user.setStatus(sysUserInfo.getStatus());
        user.setRole_id(stringRoleIds);
        user.setLogin_name(sysUserInfo.getLogin_name());
        user.setEmail(sysUserInfo.getEmail());
        user.setName(sysUserInfo.getName());
        user.setPassword(sysUserInfo.getPassword());
        user.setPhone(sysUserInfo.getPhone());
        user.setCreate_date(LocalDateTime.now());
        this.save(user);
    }

    @Override
    public SysUserInfo getWithId(String id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        User user = this.getOne(queryWrapper);
        List<String> stringRoleIds = getStringRoleIds(user);
        SysUserInfo sysUserInfo = new SysUserInfo();
        sysUserInfo.setStatus(user.getStatus());
        sysUserInfo.setRole_id(stringRoleIds);
        sysUserInfo.setId(id);
        sysUserInfo.setLogin_name(user.getLogin_name());
        sysUserInfo.setEmail(user.getEmail());
        sysUserInfo.setName(user.getName());
        sysUserInfo.setPassword(user.getPassword());
        sysUserInfo.setPhone(user.getPhone());
        sysUserInfo.setCreate_date(LocalDateTime.now());
        return sysUserInfo;
    }

    @Override
    public void updateWithSys(SysUserInfo sysUserInfo) {
        String stringRoleIds = getStringRoleIds(sysUserInfo);
        User user = new User();
        user.setStatus(sysUserInfo.getStatus());
        user.setId(sysUserInfo.getId());
        user.setRole_id(stringRoleIds);
        user.setLogin_name(sysUserInfo.getLogin_name());
        user.setEmail(sysUserInfo.getEmail());
        user.setName(sysUserInfo.getName());
        user.setPassword(sysUserInfo.getPassword());
        user.setPhone(sysUserInfo.getPhone());
        user.setUpdate_date(LocalDateTime.now());
        this.updateById(user);
    }


    private String getStringRoleIds(@RequestBody SysUserInfo sysUserInfo) {
        StringBuilder sb = new StringBuilder();
        List<String> roleIdList = sysUserInfo.getRole_id();
        sb.append("[");
        if (roleIdList != null) {

            for (int i = 0; i < roleIdList.size(); i++) {
                String roleId = roleIdList.get(i);
                sb.append("\"").append(roleId).append("\"").append(i == roleIdList.size() - 1 ? "" : ",");
            }
        }
            sb.append("]");
            return sb.toString();
        }
        private List<String> getStringRoleIds (User user){
            //查询用户角色id集合: roleIdList
            String[] roleIds = user.getRole_id().replace("[", "")
                    .replace("]", "")
                    .replace("\"", "")
                    .split(",");
            List<String> roleIdList = new ArrayList<>();
            Collections.addAll(roleIdList, roleIds);
            return roleIdList;

        }
}
